﻿namespace ITSSP.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.Web.DomainServices;
    using System.Web.DomainServices.Providers;
    using System.Web.Ria;
    using System.Web.Ria.Services;

    using ITSSP.Web.Models;


    // Implements application logic using the UserModelContainer context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    [RequiresAuthentication()]
    [EnableClientAccess()]
    public class UserService : LinqToEntitiesDomainService<UserModelContainer>
    {
        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
		
		[RequiresRole("Admin")]
        public IQueryable<User> GetUserSet()
        {
            return this.ObjectContext.UserSet;
        }
		
		[RequiresRole("Admin")]
        public IQueryable<User> GetUsers()
        {
            return this.ObjectContext.UserSet;
        }
		
		[RequiresRole("Admin")]
        public IQueryable<User> GetGuestUsers()
        {
            return from p in this.ObjectContext.UserSet
                   where !p.ActiveDirectoryUser
                   select p;
        }
		
		[RequiresRole("Admin")]
        public IQueryable<User> GetEnabledGuestUsers()
        {
            return from p in this.ObjectContext.UserSet
                   where !p.ActiveDirectoryUser && p.Enabled
                   select p;
        }

		[RequiresRole("Admin")]
        public IQueryable<User> GetADUsers()
        {
            return from p in this.ObjectContext.UserSet
                   where p.ActiveDirectoryUser
                   select p;
        }

		[RequiresRole("Admin")]
        public void InsertUser(User user)
        {
            if ((user.EntityState != EntityState.Added))
            {
                if ((user.EntityState != EntityState.Detached))
                {
                    this.ObjectContext.ObjectStateManager.ChangeObjectState(user, EntityState.Added);
                }
                else
                {
                    this.ObjectContext.AddToUserSet(user);
                }
            }
        }

		[RequiresRole("Admin")]
        public void UpdateUser(User currentUser)
        {
            if ((currentUser.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentUser, this.ChangeSet.GetOriginal(currentUser));
            }
        }

		[RequiresRole("Admin")]
        public void DeleteUser(User user)
        {
            if ((user.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(user);
            }
            this.ObjectContext.DeleteObject(user);
        }

		[Invoke]
		[RequiresRole("Guest")]
		public void ChangePassword(string oldPassword, string newPassword)
		{
			User currentUser = (from u in this.ObjectContext.UserSet
								where u.Login == ServiceContext.User.Identity.Name
								select u).FirstOrDefault();

			if (!currentUser.authenticate(oldPassword)) {
				throw new AuthenticationFailedException("Old password did not match.");
			}

			currentUser.HashedPassword = User.generateNewPasswordHash(newPassword);
			this.ObjectContext.SaveChanges();
		}
    }
}


